package com.blrs.service.contract.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.ContractManagementInfo;
import com.blrs.domain.ContractReviewInfo;
import com.blrs.domain.EnterpriseInfo;
import com.blrs.domain.HighSeas;
import com.blrs.mapper.*;
import com.blrs.service.contract.IContractReviewInfoService;
import liquibase.pro.packaged.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-30
 */
@Service
public class ContractReviewInfoServiceImpl implements IContractReviewInfoService {
    @Autowired
    private ContractReviewInfoMapper contractReviewInfoMapper;
    @Autowired
    private ContractManagementInfoMapper contractManagementInfoMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private HighSeasMapper highSeasMapper;
    @Autowired
    private ElectricSalesInfoMapper electricSalesInfoMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param reviewId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ContractReviewInfo selectContractReviewInfoByReviewId(String reviewId) {
        return contractReviewInfoMapper.selectContractReviewInfoByReviewId(reviewId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param contractReviewInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ContractReviewInfo> selectContractReviewInfoList(ContractReviewInfo contractReviewInfo) {
        return contractReviewInfoMapper.selectContractReviewInfoList(contractReviewInfo);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param contractReviewInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertContractReviewInfo(ContractReviewInfo contractReviewInfo) {
        ContractManagementInfo managementInfo = contractManagementInfoMapper.selectContractManagementInfoByContractId(contractReviewInfo.getContractId());
        if ("0".equals(managementInfo.getState()) && "0".equals(managementInfo.getSign())) {
            contractReviewInfoMapper.deleteContractReviewInfoByContractId(contractReviewInfo.getContractId());
        }
        if("0".equals(managementInfo.getState()) && "1".equals(managementInfo.getSign())){
            ContractReviewInfo reviewInfo = new ContractReviewInfo();
            reviewInfo.setContractId(contractReviewInfo.getContractId());
            List<ContractReviewInfo> reviewInfoList = contractReviewInfoMapper.selectContractReviewInfoList(reviewInfo);
           for(int i = 0;i<reviewInfoList.size();i++){
               if(StringUtils.isEmpty(reviewInfoList.get(i).getReviewComments())){
                    contractReviewInfoMapper.deleteContractReviewInfoByReviewId(reviewInfoList.get(i).getReviewId());
               }
           }
        }
        managementInfo.setCovenantId("");
        managementInfo.setCovenantId(contractReviewInfo.getReviewId());
        contractManagementInfoMapper.updateContractManagementInfo(managementInfo);
        return contractReviewInfoMapper.insertContractReviewInfo(contractReviewInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addContractReviewInfo(ContractReviewInfo contractReviewInfo) {
        int count = 0;
        ContractReviewInfo reviewInfo = new ContractReviewInfo();
        reviewInfo.setContractId(contractReviewInfo.getContractId());
        List<ContractReviewInfo> reviewInfoList = contractReviewInfoMapper.selectContractReviewInfoList(reviewInfo);
        for (int i = 0; i < reviewInfoList.size(); i++) {
            if (StringUtils.isEmpty(reviewInfoList.get(i).getReviewResult())) {
                ContractReviewInfo info = contractReviewInfoMapper.selectContractReviewInfoByReviewId(reviewInfoList.get(i).getReviewId());
                info.setReviewComments(contractReviewInfo.getReviewComments());
                info.setReviewResult(contractReviewInfo.getReviewResult());
                info.setReviewTime(new Timestamp(new Date().getTime()));
                count = contractReviewInfoMapper.updateContractReviewInfo(info);
            }
        }
        if ("0".equals(contractReviewInfo.getReviewResult())) {
            ContractManagementInfo managementInfo = contractManagementInfoMapper.selectContractManagementInfoByContractId(contractReviewInfo.getContractId());
            String reviewer = managementInfo.getContractReviewer() == null ? "" : managementInfo.getContractReviewer() + ",";
            if (StringUtils.isNotEmpty(managementInfo.getContractReviewer())) {
                managementInfo.setContractReviewer(reviewer + SecurityUtils.getUsername());
                if (managementInfo.getContractReviewer().indexOf(contractReviewInfo.getReviewer()) != -1) {
                    String newReviewer = managementInfo.getContractReviewer().replace(contractReviewInfo.getReviewer(), "");
                    managementInfo.setContractReviewer(newReviewer);
                } else {
                    managementInfo.setContractReviewer(reviewer + SecurityUtils.getUsername());
                }
            } else {
                if(contractReviewInfo.getReviewer().equals(SecurityUtils.getUsername())){
                    managementInfo.setContractReviewer("");
                }else{
                    managementInfo.setContractReviewer(reviewer + SecurityUtils.getUsername());
                }
            }

            //把该合同的合同信息的客户资料补充到客户管理中
            //1.先去客户查询，如果客户管理中有对应的客户，则不动
            Long number = enterpriseInfoMapper.number(managementInfo.getEnterpriseName(), String.valueOf(SecurityUtils.getOrgId()));
            if (number == null) {
                //2.从公海中查询，把公海中查询到的信息，同步到客户中，然后删除公海和电销
                HighSeas seas = highSeasMapper.selectEnterpriseInfoByNameInfo(managementInfo.getEnterpriseName(), SecurityUtils.getOrgId());
                EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
                //手动
                enterpriseInfo.setEnterpriseId(seas.getEnterpriseId());
               enterpriseInfo.setEnterpriseName(seas.getEnterpriseName());
               enterpriseInfo.setEnterpriseSource(seas.getEnterpriseSource());
               enterpriseInfo.setEnterpriseAbbreviation(seas.getEnterpriseAbbreviation());
                   enterpriseInfo.setEnterpriseNature(seas.getEnterpriseNature());
                   enterpriseInfo.setEnterpriseAddress(seas.getEnterpriseAddress());
                 enterpriseInfo.setAdministrativeArea(seas.getAdministrativeArea());
               enterpriseInfo.setLegalRepresentative(seas.getLegalRepresentative());
               enterpriseInfo.setRegisteredCapital(seas.getRegisteredCapital());
               enterpriseInfo.setIndustry(seas.getIndustry());
               enterpriseInfo.setContacts(seas.getContacts());
               enterpriseInfo.setTelephone(seas.getTelephone());
              enterpriseInfo.setDevelopmentId(seas.getDevelopManager());
              enterpriseInfo.setTransactionId(seas.getBusinessManager());
              enterpriseInfo.setDevelopmentManager(seas.getDevelopmentName());
             enterpriseInfo.setBusinessManager(seas.getBusinessManagerName());
            enterpriseInfo.setEnterpriseAddress(seas.getEnterpriseAddress());
            enterpriseInfo.setBusinessScope(seas.getBusinessScope());
            enterpriseInfo.setMarkAs("1");
            enterpriseInfo.setBelongTo(String.valueOf(SecurityUtils.getOrgId()));
             enterpriseInfo.setCreationTime(new Timestamp(new Date().getTime()));
             enterpriseInfo.setOperateTime(new Timestamp(new Date().getTime()));
             enterpriseInfo.setOperateId(SecurityUtils.getLoginUser().getUserId());
             enterpriseInfo.setDepartmentId(SecurityUtils.getLoginUser().getDeptId());
             enterpriseInfo.setDepartmentName(SecurityUtils.getLoginUser().getOrgName());
             enterpriseInfo.setOrganizationId(SecurityUtils.getLoginUser().getOrgId());
             enterpriseInfo.setOrganizationName(SecurityUtils.getLoginUser().getOrgName());
            enterpriseInfo.setDelFlag("0");
            enterpriseInfo.setStatus("1");
           enterpriseInfo.setCreationId(String.valueOf(SecurityUtils.getLoginUser().getUserId()));
          enterpriseInfo.setCreationName(SecurityUtils.getLoginUser().getUser().getNickName());
          enterpriseInfo.setIsShare("0");
          enterpriseInfo.setOperateStatus("0");
          enterpriseInfoMapper.insertEnterpriseInfo(enterpriseInfo);
          //3.删除公海电销信息
                electricSalesInfoMapper.deleteElectricSalesInfoByElectricId(managementInfo.getEnterpriseId());
                highSeasMapper.deleteHighSeasByEnterpriseId(managementInfo.getEnterpriseId());
            }
            contractManagementInfoMapper.updateContractManagementInfo(managementInfo);
            ContractReviewInfo review = new ContractReviewInfo();
            review.setReviewId(contractReviewInfo.getReviewId());
            review.setContractId(contractReviewInfo.getContractId());
            review.setReviewerId(contractReviewInfo.getReviewerId());
            review.setReviewer(contractReviewInfo.getReviewer());
            count = contractReviewInfoMapper.insertContractReviewInfo(review);
        }
        if ("1".equals(contractReviewInfo.getReviewResult())) {
            ContractManagementInfo managementInfo = contractManagementInfoMapper.selectContractManagementInfoByContractId(contractReviewInfo.getContractId());
            managementInfo.setSign("1");
            managementInfo.setCovenantId("");
            managementInfo.setState("0");
            managementInfo.setContractReviewer("");
            contractManagementInfoMapper.updateContractManagementInfo(managementInfo);
        }
        if ("2".equals(contractReviewInfo.getReviewResult())) {
            ContractManagementInfo managementInfo = contractManagementInfoMapper.selectContractManagementInfoByContractId(contractReviewInfo.getContractId());
            managementInfo.setState("2");
            managementInfo.setSign("2");
            managementInfo.setContractReviewer(managementInfo.getContractReviewer() + "," + SecurityUtils.getUsername());
            contractManagementInfoMapper.updateContractManagementInfo(managementInfo);
        }
        if ("3".equals(contractReviewInfo.getReviewResult())) {
            ContractManagementInfo managementInfo = contractManagementInfoMapper.selectContractManagementInfoByContractId(contractReviewInfo.getContractId());
            managementInfo.setState("3");
            managementInfo.setSign("3");
            managementInfo.setContractReviewer(managementInfo.getContractReviewer() + "," + SecurityUtils.getUsername());
            contractManagementInfoMapper.updateContractManagementInfo(managementInfo);
        }
        return count;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param contractReviewInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateContractReviewInfo(ContractReviewInfo contractReviewInfo) {
        return contractReviewInfoMapper.updateContractReviewInfo(contractReviewInfo);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param reviewIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteContractReviewInfoByReviewIds(String[] reviewIds) {
        return contractReviewInfoMapper.deleteContractReviewInfoByReviewIds(reviewIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param reviewId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteContractReviewInfoByReviewId(String reviewId) {
        return contractReviewInfoMapper.deleteContractReviewInfoByReviewId(reviewId);
    }
}
